package com.example.exceldemo.utils;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
public class EntityUtils {


    public static <T> List<Map<String,Object>> convertListBean2ListMap(List<T> beanList) {
        List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
        for(int i=0, n=beanList.size(); i<n; i++){
            Object bean = beanList.get(i);
            Map<String,Object> map = objectToMaps(bean);
            mapList.add(map);
        }
        return mapList;
    }
	
	public static Map<String, Object> objectToMaps(Object obj)  {
        if(obj == null){    
            return null;    
        }   
        Map<String, Object> map = new HashMap<String, Object>();    
        Field[] declaredFields = obj.getClass().getDeclaredFields();    
        for (Field field : declaredFields) {    
            field.setAccessible(true);
            try {
                map.put(field.getName(), field.get(obj));
            }catch (Exception e){
                log.info("EntityUtils.objectToMaps,错误：{}",e);
            }

        }    
        return map;  
    }   


}